﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace TodayWhoModel
{
    public class ImageHelper
    {
        public ImageHelper()
        {

        }

        public Image Resize(System.Drawing.Image img, int width, int maxHeight)
        {
            //keeps aspect ratio
            //resizes so that newWidth = width
            //but if newHeight > max height after resizing, resizes according to maxHeight

            //get the original heights of the image
            int originalHeight = img.Height;
            int originalWidth = img.Width;
            int newWidth, newHeight;

            newHeight = originalHeight * width / originalWidth;
            if (newHeight > maxHeight)
            {
                //resize according to height such that newHeight = maxHeight
                newHeight = maxHeight;
                newWidth = originalWidth * maxHeight / originalHeight;
            }
            else
            {
                //resize according to width such that newWidth = width
                newWidth = width;
            }

            Bitmap bmp = new Bitmap(newWidth, newHeight);
            Graphics graphic = Graphics.FromImage((System.Drawing.Image)bmp);
            graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphic.DrawImage(img, 0, 0, newWidth, newHeight);
            graphic.Dispose();

            return (System.Drawing.Image)bmp;

        }

        public void CreateRoundedImage(string path, string savePath)
        {
            int roundedDia = 50;
            using (System.Drawing.Image imgin = System.Drawing.Image.FromFile(path))
            {
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(imgin.Width, imgin.Height);
                Graphics g = Graphics.FromImage(bitmap);
                g.Clear(Color.White);
                Brush brush = new System.Drawing.TextureBrush(imgin);
                FillRoundedRectangle(g, new Rectangle(0, 0, imgin.Width, imgin.Height), roundedDia, brush);
                // done with drawing dispose graphics object.
                g.Dispose();
                // Stream Image to client.
                Image img = (Image)bitmap;
                img.Save(savePath);
                // dispose bitmap object.

                bitmap.Dispose();
            }
        }

        public static void FillRoundedRectangle(Graphics g, Rectangle r, int d, Brush b)
        {
            // anti alias distorts fill so remove it.
            System.Drawing.Drawing2D.SmoothingMode mode = g.SmoothingMode;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
            g.FillPie(b, r.X, r.Y, d, d, 180, 90);
            g.FillPie(b, r.X + r.Width - d, r.Y, d, d, 270, 90);
            g.FillPie(b, r.X, r.Y + r.Height - d, d, d, 90, 90);
            g.FillPie(b, r.X + r.Width - d, r.Y + r.Height - d, d, d, 0, 90);
            g.FillRectangle(b, r.X + d / 2, r.Y, r.Width - d, d / 2);
            g.FillRectangle(b, r.X, r.Y + d / 2, r.Width, r.Height - d);
            g.FillRectangle(b, r.X + d / 2, r.Y + r.Height - d / 2, r.Width - d, d / 2);
            g.SmoothingMode = mode;
        }
    }

}
